<?php
declare(strict_types=1);

namespace App\Http\Model\TransactionDetail;

use Illuminate\Support\Facades\DB;
use App\Http\Library\Excel;
use App\Http\ModelDao\ClassTransactionDetail\ClassTransactionDetailDao;
use App\Enum\ClassListHistoryEnum;


class ClassTransactionDetailModel
{
    /*数据集合*/
    private static $rows;
    /*数据长度*/
    private static $rowsCount = 0;
    /*查询字段*/
    private static $fileds = [
        'id',
        'class_netting',
        'account_netting',
        'create_date',
        'result',
        'class_id',
        'class_name',
        'ledger_subject_name',
        'city_name',
        'pay_num',
        'pay_amt',
        'pay_fee',
        'refund_num',
        'refund_amt',
        'refund_fee'
    ];
    /*
     * @param array $params
     *
     * return array
     */
    public static function index($params = []):array{

            $limit =  (int)$params["pagesize"];
            $offset = ((int)$params["page"]-1)*$limit;
            $where = [];
            /*班级名称*/
            if(!isEmpty($params["class_name"])){
                $where[] = ["class_name","=",$params["class_name"]];
            }
            /*推送状态*/
            if(!isEmpty($params["result"])){
                $where[] = ["result","=",$params["result"]];
            }
            /*城市名称*/
            if(!isEmpty($params["city_name"])){
                $where[] = ["city_name","=",$params["city_name"]];
            }
            /*主体名称*/
            if(!isEmpty($params["ledger_subject_name"])){
                $where[] = ["ledger_subject_name","=",$params["ledger_subject_name"]];
            }
            /*汇总时间-区间*/
             if(!isEmpty($params["collect_ctime"]) && !isEmpty($params["collect_etime"])){
                $where[] = ["create_date",">=",$params["collect_ctime"]];
                $where[] = ["create_date","<=",$params["collect_etime"]];
            }


            //DB::connection()->enableQueryLog();#开启执行日志

            //download select
            if($params["download"] == 'All'){
                /*获取全部数据*/
                self::$rows = DB::table("class_list_pay_result_history")->wherein('pfs_dw_id',$params['pfs_dw_id'])->where($where)->orderBy("id","desc")->get(self::$fileds);
                self::$rowsCount = sizeof(self::$rows);
            }else{
                self::$rows = DB::table("class_list_pay_result_history")->wherein('pfs_dw_id',$params['pfs_dw_id'])->where($where)->orderBy("id","desc")->offset($offset)->limit($limit)->get(self::$fileds);
                self::$rowsCount = DB::table("class_list_pay_result_history")->wherein('pfs_dw_id',$params['pfs_dw_id'])->where($where)->count();
            }

            //print_r(DB::getQueryLog());   //获取查询语句、参数和执行时间

        self::download($params);

        return [
            'list' => self::$rows,
            'allRows' => self::$rowsCount,
        ];
    }
    /*
     * 数据下载
    */
    private static function download($params){
        //
        if(in_array($params["download"],['All','Current'])){
            Excel::$download = true;
            Excel::$rowWidth = [
                'A:A' => 12,
                'B:B' => 16,
                'C:C' => 22,
                'D:D' => 15,
                'E:E' => 11,
                'F:F' => 14,
                'G:G' => 19,
                'H:H' => 17,
                'I:I' => 14,
                'J:J' => 14,
                'K:K' => 17,
                'L:L' => 19,
                'M:M' => 19,
                'N:N' => 12,
            ];
            $title = ['城市','主体','班级ID','班级名称','汇总时间','当日缴费笔数','当日缴费课程金额','当日缴费手续费','当日退费笔数','当日退费金额','当日退费手续费','当日班级轧差净额','当日账户轧差净额','推送状态'];
            //$title = ['a','b','c','d'];
            Excel::$title = $title;

           //var_dump(ClassTransactionDetailDao::numberResult(self::$rows));exit();
            Excel::send(ClassTransactionDetailDao::numberResult(self::$rows),'班级交易明细-'.date("Ymd-His").'.xlsx');
             exit();
        }
    }
    /*
     * 判断是否同意推送
     * 根据一组ID去判断数组存不存在结果不等于FAILURE 失败的数据
     * 如果条数大于0,则不允许推送，如果条数等于0，则允许推送
     *
     * @params Array $ids [1,2,3,4]
     * return Bool
     */
    public static function isAgreePush(Array $ids):bool{
        $count = DB::table("class_list_pay_result_history")
            ->whereIn("id",$ids)
            ->where('result','!=',ClassListHistoryEnum::RESULT_FAILURE)
            ->count();

        return $count>0 ? false : true;
    }
}
